﻿@{
    ViewBag.Title = "Phần quyền chức năng cho User";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@model GPS.MODELS.RoleAddMenuViewModel

@helper ShowTree(System.Collections.Generic.List<GPS.MODELS.RoleAddMenuViewModel> Model)
{

    foreach (var item in Model)
    {
        var i = "_";

        <li class="clickable">
            <input type="checkbox" name="chk_@item.Item_ID" value="@item.Item_ID@i@item.Parent_ID" @item._checked /> @item.Item_name

            @if (item.Childens.Any())
            {
                <ul style="list-style:none" class="sf-menu-sub">
                    @ShowTree(item.Childens)
                </ul>
            }

        </li>
    }
}


<div id="tabs-1">
    <h3 class="text-center fdb-title-search-form">Phần quyền chức năng cho người dùng @ViewBag.UserName</h3>
    <label style="color:blue">@ViewBag.SuccessUser</label>
    @Html.Raw(ViewBag.Error)
    @using (Html.BeginForm("UserAddMenu", "Account", FormMethod.Post, new { @id = "formCreate" }))
    {
        @Html.AntiForgeryToken()
        @Html.Hidden("UserName", (string)ViewBag.UserName)
        @Html.Hidden("UserId", (string)ViewBag.UserId)

        <fieldset class="fs_body">
            <legend class="head_title">Chọn chức năng</legend>
            <div id="tree">
                <ul style="list-style:none" id="nav">
                    <li class="clickable">
                        <input type="checkbox" id="chkAll" /> Tất cả
                        <ul style="list-style:none" class="sf-menu-sub">
                            @ShowTree(Model.Childens)
                        </ul>
                    </li>
                </ul>
            </div>


        </fieldset>
    <div class="form-group Action_FormDetail row">
        <button type="submit" class="btn btn-danger btn-sm">
            <span class="glyphicon glyphicon-floppy-disk"></span> Cập nhật
        </button>
        &nbsp;
        @Html.ActionLink("Quay lại", "Index", "Account", null, new { @class = "btn btn-primary btn-sm" })
    </div>
    }
</div>


<script src="~/Scripts/jquery-ui-1.11.4.js"></script>

<style>
    div#tree ul li {
        margin-left: 25px;
        position: relative;
        padding-left: 5px;
    }

        div#tree ul li::before {
            content: " ";
            position: absolute;
            width: 1px;
            background-color: #ccc;
            top: 5px;
            bottom: -12px;
            left: -10px;
        }



        div#tree ul li:last-child::before {
            display: none;
        }

        div#tree ul li::after {
            content: " ";
            position: absolute;
            left: -10px;
            width: 10px;
            height: 1px;
            background-color: #ccc;
            top: 12px;
        }
</style>
<script type="text/javascript">
    $(document).ready(function () {

        var countVal = 0;
        var countAll = 0;
        $("#tree").find('input[type=checkbox]:not(#chkAll)').each(function () {

            if ($(this).is(':checked')) {
                countVal++;
            }

            countAll++;
        });
        if (countAll == countVal)
            $("#chkAll").prop('checked', true);
        else
            $("#chkAll").prop('checked', false);


        //check all child sẽ check luôn parent
        $('li :checkbox').on('click', function () {
            var $chk = $(this),
                $li = $chk.closest('li'),
                $ul, $parent;
            if ($li.has('ul')) {
                $li.find(':checkbox').not(this).prop('checked', this.checked)
            }
            do {
                $ul = $li.parent();
                $parent = $ul.siblings(':checkbox');
                if ($chk.is(':checked')) {
                    $parent.prop('checked', $ul.has(':checkbox:not(:checked)').length == 0)
                } else {
                    $parent.prop('checked', false)
                }
                $chk = $parent;
                $li = $chk.closest('li');
            } while ($ul.is(':not(.someclass)'));
        });




    });
</script>
